import { sizeExpr } from './bizUnit';
import { LineLayer } from '../map/layers/LineLayer';
import logger from 'loglevel';
import { IBizUnitConfig, ModelObjType } from '../shared/types';
import { EngiGis } from '..';
import { SharedTextLayer } from './shared/sharedTextLayer';
import { ModelBizUnit } from './modelBizUnit';

export class PipelineBizUnit extends ModelBizUnit {
  objType: ModelObjType = 'pipeline'; // 业务单元类型
  unitType: string = this.objType;
  override sizeScale = 2;

  constructor(engiGis: EngiGis, sharedTextLayer: SharedTextLayer) {
    super(engiGis, sharedTextLayer);
  }

  async setRenderer(config: IBizUnitConfig): Promise<LineLayer[]> {
    logger.info('开始绘制管线图层');
    // 绘制管线图层
    const lineWidth = sizeExpr(this.sizeScale);
    const lineLayer = new LineLayer(
      {
        layerId: this.layerId,
        sourceId: this.sourceId,
        map: this.map,
        sort: config.sort || 0,
      },
      {
        layout: {
          'line-cap': 'round',
          'line-join': 'round',
          'line-miter-limit': 2,
          'line-round-limit': 1,
          'line-sort-key': config.sort || 0,
        },
        paint: {
          'line-color': [
            'match',
            ['get', 'rt_state'],
            'normal',
            '#acacab',
            'warning',
            '#FFFF00',
            'error',
            '#FF0000',
            '#acacab', // 默认颜色
          ],
          'line-width': lineWidth,
          'line-opacity': 0.8,
        },
        filter: ['>', ['zoom'], ['get', 'minZ']],
      },
    );
    return [lineLayer];
  }
}
